package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 保存 或 取消 不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        if (dto.getArticleId()==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"文章id不能为空");
        }
        //得到登录用户的id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getType().intValue()!= 0&&dto.getType().intValue()!= 1 ){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"点赞方式只能是0或者1");
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());

        if (dto.getType().intValue()== 0){
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApUnlikesBehavior one = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if (one!=null){
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"不能重复喜欢");
            }
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setEntryId(apBehaviorEntry.getId());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setType((short) 0);
            apUnlikesBehavior.setCreatedTime(apBehaviorEntry.getCreatedTime());
            mongoTemplate.save(apUnlikesBehavior);
        }else {
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApUnlikesBehavior one = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if (one == null){
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"先点不喜欢,才能取消");
            }
            mongoTemplate.remove(query,ApUnlikesBehavior.class);
        }

        return ResponseResult.okResult();
    }
}
